from presenter import basepresenter

class ArticleCategoryPresenter(basepresenter.BasePresenter):

    def respondToGet(self):
        # set up defaults
        page = 0
        items_per_page = self.settings.articlesperpage
        category = None

        # get the page vars
        if (self.request.get('category')):
            category = self.request.get('category')
            self.template_values['category'] = category
        if (self.request.get('page')):
            page = int(self.request.get('page'))
            self.template_values['page'] = str(page)
        if (self.request.get('items_per_page')):
            items_per_page = int(self.request.get('items_per_page'))
        self.template_values['items_per_page'] = str(items_per_page)

        #get the articles for the category
        articles = self.articletask.getRange(page, items_per_page, category)
        self.template_values['articles'] = articles

        # set up the page vars
        if (self.articletask.hasNextPage()):
            self.template_values['nextpage'] = str(page + 1)
        if (self.articletask.hasPrevPage()):
            self.template_values['prevpage'] = str(page - 1)

        # set up the page title
        page_title = self.sitetitle + ' - Articles by category - ' + category
        self.template_values['page_title'] = page_title

        #get the view to draw the page
        self.view.showGetResponse(self.template_values)